package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysWallpaper;
import com.ruoyi.system.domain.vo.PacketLogVO;
import com.ruoyi.system.mapper.SysWallpaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysPacketLogMapper;
import com.ruoyi.system.domain.SysPacketLog;
import com.ruoyi.system.service.ISysPacketLogService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-22
 */
@Service
public class SysPacketLogServiceImpl implements ISysPacketLogService {
    @Autowired
    private SysPacketLogMapper sysPacketLogMapper;

    @Autowired
    private SysWallpaperMapper wallpaperMapper;


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysPacketLog selectSysPacketLogById(Long id) {
        return sysPacketLogMapper.selectSysPacketLogById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysPacketLog 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysPacketLog> selectSysPacketLogList(SysPacketLog sysPacketLog) {
        return sysPacketLogMapper.selectSysPacketLogList(sysPacketLog);
    }

    @Override
    public List<PacketLogVO> queryPacketLogList(PacketLogVO vo) {
        List<PacketLogVO> list = sysPacketLogMapper.queryPacketLogList(vo);
        for (PacketLogVO vo1 : list) {
            PacketLogVO m = new PacketLogVO();
            m.setOs(vo1.getOs());
            m.setPhone(vo1.getPhone());
            m.setModel(vo1.getModel());
            vo1.setClickNumber(sysPacketLogMapper.getClickNumber(m));
            vo1.setDownloadNumber(sysPacketLogMapper.getDownloadNumber(m));
        }
        return list;
    }

    @Override
    public List<PacketLogVO> queryPacketLogTypeList(PacketLogVO vo) {
        return sysPacketLogMapper.queryPacketLogList(vo);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysPacketLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysPacketLog(SysPacketLog sysPacketLog) {
        sysPacketLog.setCreateTime(DateUtils.getNowDate());
        if (sysPacketLog.getIsDownload() > 0) {
            sysPacketLog.setDownloadTime(DateUtils.getNowDate());
            SysWallpaper sysWallpaper = wallpaperMapper.selectSysWallpaperById(sysPacketLog.getWallpaperId());
            if (sysWallpaper.getDownloadNumber() != null) {
                sysWallpaper.setDownloadNumber(sysWallpaper.getDownloadNumber() + 1);
            } else {
                sysWallpaper.setDownloadNumber(1l);
            }
            wallpaperMapper.updateSysWallpaper(sysWallpaper);
        }
        return sysPacketLogMapper.insertSysPacketLog(sysPacketLog);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysPacketLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysPacketLog(SysPacketLog sysPacketLog) {
        return sysPacketLogMapper.updateSysPacketLog(sysPacketLog);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysPacketLogByIds(Long[] ids) {
        return sysPacketLogMapper.deleteSysPacketLogByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysPacketLogById(Long id) {
        return sysPacketLogMapper.deleteSysPacketLogById(id);
    }

    @Override
    public List<Map<String, Object>> queryPiePacketLogData() {
        List<Map<String, Object>> map = new ArrayList<>();
        List<PacketLogVO> mapList = sysPacketLogMapper.queryPiePacketLogData();
        List<String> titleData = new ArrayList<>();
        List<Long> barChartData = new ArrayList<>();
        for (PacketLogVO m : mapList) {
            if (m.getModel() != null && m.getPhone() != null) {
                titleData.add(m.getPhone().replaceAll("\\s*", "") + "_" + m.getModel().replaceAll("\\s*", ""));
                barChartData.add(m.getNumber());
            }
        }
        Map<String, Object> p = new HashMap<>();
        p.put("titleData", titleData);
        p.put("barChartData", barChartData);
        map.add(p);
        return map;
    }

    @Override
    public List<Map<String, Object>> getPacketLineData() {
        List<Map<String, Object>> map = new ArrayList<>();
        List<PacketLogVO> click = sysPacketLogMapper.getLineClickNumberData();
        List<PacketLogVO> download = sysPacketLogMapper.getLineDownloadNumberData();
        Map<String, Object> m = new HashMap<>();
        List<String> list = new ArrayList<>();
        List<Long> clickNumberData = new ArrayList<>();
        List<Long> downloadNumberData = new ArrayList<>();
        for (PacketLogVO vo : click) {
            list.add(vo.getCreateTime());
            clickNumberData.add(vo.getNumber());
            List<PacketLogVO> ll = download.stream().filter(a -> a.getCreateTime().equals(vo.getCreateTime())).collect(Collectors.toList());
            if (ll != null && ll.size() > 0) {
                downloadNumberData.add(ll.get(0).getNumber());
            } else {
                downloadNumberData.add(0l);
            }
        }
        m.put("titleData", list);
        m.put("clickNumberData", clickNumberData);
        m.put("downloadNumberData", downloadNumberData);
        map.add(m);
        return map;
    }
}
